package RequestHandler;

import Request.*;
import DBControl.*;

public class RequestHandler {
    private static RequestHandler instance = new RequestHandler();
    
    public static int idle = 100;
    private SessionList sessionList;
    private RequestQueue requestQueue;
    private WorkerPool workerPool;
    private Dispatcher dispatcher;
    private DBInterface dbi;
    
    public static RequestHandler getInstance() {
        return instance;
    }
    
    private RequestHandler() {
        init();
    }
    
    private void init() {
        sessionList = new SessionList();
        requestQueue = new RequestQueue();
        dbi = new SimpleJDBC();
        workerPool = new WorkerPool(sessionList, dbi);
        dispatcher = new Dispatcher(requestQueue, workerPool);
        dispatcher.start();
    }
    
    public void handleRequest(Request request) {
        System.out.println(request.getRequestType());
        requestQueue.addRequest(request);
    }
    
    public void stop() {
        dispatcher.quit();
        try {
            dispatcher.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        workerPool.stop();
    }
    
    public void stopSession(int sid) {
    	sessionList.deleteSession(sid);
    }
}